/**
 * Copyright (C), 2019-2023,
 * FileName: MessageService
 * Author:   fanxquer
 * Date:     2023/7/10 14:22
 * Description: 站内信
 * History:
 * <author>          <time>          <version>          <desc>
 * 邱富康           2023/7/10           1.0
 */
package com.hsteach.service.third;

import com.alibaba.fastjson2.JSONObject;
import com.hsteach.common.core.domain.model.AuthInfo;
import com.hsteach.common.utils.SecurityUtils;
import com.hsteach.common.utils.StringUtils;
import com.hsteach.enums.MsgTypeEnum;
import com.hsteach.framework.feign.ClientConstant;
import com.hsteach.framework.feign.client.auth.dto.AuthResp;
import com.hsteach.framework.feign.client.message.MessageClient;
import com.hsteach.framework.feign.client.message.dto.AddTodothingsEntity;
import com.hsteach.framework.feign.client.message.dto.ReceivePerson;
import com.hsteach.framework.feign.client.message.dto.SendMsgEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 〈站内信〉
 *
 * @author Qiu Fukang
 * @create 2023/7/10
 * @since 1.0.0
 */
@Service
public class MessageService {

    @Autowired
    private MessageClient messageClient;

    public boolean sendMsg(String msg, List<String> personIds, MsgTypeEnum msgType, String title){
        AuthInfo authInfo = SecurityUtils.getAuthInfo();
        SendMsgEntity msgEntity = new SendMsgEntity();
        msgEntity.setContent(msg);
        List<ReceivePerson> persons = personIds.stream().map(e -> {
            ReceivePerson person = new ReceivePerson();
            person.setReceiveUserId(Long.valueOf(e));
            person.setReceiveOrganizeId(authInfo.getOrganizeId());
            return person;
        }).collect(Collectors.toList());
        msgEntity.setReceiverList(persons);
//        msgEntity.setComeFromApplication(msgType.getComeFromApplication());
        msgEntity.setCategory(msgType.getCategory());
        if (StringUtils.isEmpty(title)) {
            msgEntity.setTitle(msgType.getTitle());
        } else {
            msgEntity.setTitle(title);
        }
        msgEntity.setComeFromModule(msgType.getComeFromModule());
        msgEntity.setComeFromAppId(authInfo.getAppId());

        AuthResp<JSONObject> resp = messageClient.send(SecurityUtils.getAuthInfo().getAccessToken()
                ,SecurityUtils.getAuthInfo().getAppToken()
                ,ClientConstant.AUTH_OPERATE_CODE_DEFAULT, msgEntity);

        return isSuccess(resp);
    }

    public boolean sendMsgByTPersonId(String msg, List<String> personIds, MsgTypeEnum msgType, String title){
        AuthInfo authInfo = SecurityUtils.getAuthInfo();
        SendMsgEntity msgEntity = new SendMsgEntity();
        msgEntity.setContent(msg);
        List<ReceivePerson> persons = personIds.stream().map(e -> {
            ReceivePerson person = new ReceivePerson();
            person.setReceivePersonId(Long.valueOf(e));
            person.setReceiveOrganizeId(authInfo.getOrganizeId());
            return person;
        }).collect(Collectors.toList());
        msgEntity.setReceiverList(persons);
//        msgEntity.setComeFromApplication(msgType.getComeFromApplication());
        msgEntity.setCategory(msgType.getCategory());
        if (StringUtils.isEmpty(title)) {
            msgEntity.setTitle(msgType.getTitle());
        } else {
            msgEntity.setTitle(title);
        }
        msgEntity.setComeFromModule(msgType.getComeFromModule());
        msgEntity.setComeFromAppId(authInfo.getAppId());

        AuthResp<JSONObject> resp = messageClient.send(SecurityUtils.getAuthInfo().getAccessToken()
                ,SecurityUtils.getAuthInfo().getAppToken()
                ,ClientConstant.AUTH_OPERATE_CODE_DEFAULT, msgEntity);

        return isSuccess(resp);
    }

    public String addTodothings(String titile, String userId, Long organizeId, String url, MsgTypeEnum msgType){
        AuthInfo authInfo = SecurityUtils.getAuthInfo();
        AddTodothingsEntity entity = new AddTodothingsEntity();
        entity.setComeFromAppId(authInfo.getAppId());
        entity.setCategory(msgType.getCategory());
        entity.setTitle(titile);
        entity.setComeFromModule(msgType.getComeFromModule());
        entity.setUserId(userId);
        entity.setOrganizeId(organizeId);
        entity.setAppContentUrl(url);

        AuthResp<JSONObject> resp = messageClient.addTodothings(SecurityUtils.getAuthInfo().getAccessToken()
                ,SecurityUtils.getAuthInfo().getAppToken()
                ,ClientConstant.AUTH_OPERATE_CODE_DEFAULT, entity);
        if (isSuccess(resp)) {
            return resp.getResult().getString("id");
        }

        return null;
    }

    public boolean dealTodothings(String id){

        AuthResp<JSONObject> resp = messageClient.dealTodothings(SecurityUtils.getAuthInfo().getAccessToken()
                ,SecurityUtils.getAuthInfo().getAppToken()
                ,ClientConstant.AUTH_OPERATE_CODE_DEFAULT, id);

        return isSuccess(resp);
    }

    private boolean isSuccess(AuthResp resp) {
        if (ClientConstant.AUTH_SUCCESS.equals(resp.getStatus())) {
            return true;
        }
        return false;
    }
}